﻿using System;
using System.Data.SqlClient;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace LiveTissWCF
{
    public static class Util
    {
        #region Authentication
        /// <summary>
        /// Chave AES
        /// </summary>
        private static byte[] Key { get; set; }
        /// <summary>
        /// Valores Iniciais do AES
        /// </summary>
        private static byte[] IV { get; set; }
        /// <summary>
        /// Converte uma string de valores hexadecimal para array de byte correspondente
        /// </summary>
        /// <param name="HexString">String de valores hexadecimal</param>
        /// <returns>Array de bytes correspondente a string</returns>
        private static byte[] ToByteArray(string HexString)
        {
            int NumberChars = HexString.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars; i += 2)
            {
                bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
            }
            return bytes;
        }
        /// <summary>
        /// Criptografa a string s usando AES
        /// </summary>
        /// <param name="s">String para criptografar</param>
        /// <returns>String criptografada</returns>
        public static string Decrypt(string s)
        {
            byte[] cipherText = ToByteArray(s);

            // TDeclare the streams used
            // to decrypt to an in memory
            // array of bytes.
            MemoryStream msDecrypt = null;
            CryptoStream csDecrypt = null;
            StreamReader srDecrypt = null;

            // Declare the RijndaelManaged object
            // used to decrypt the data.
            AesManaged aesAlg = null;

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            try
            {
                // Create a RijndaelManaged object
                // with the specified key and IV.
                aesAlg = new AesManaged();
                aesAlg.Key = Key;
                aesAlg.IV = IV;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor();

                // Create the streams used for decryption.
                msDecrypt = new MemoryStream(cipherText);
                csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
                srDecrypt = new StreamReader(csDecrypt);

                // Read the decrypted bytes from the decrypting stream
                // and place them in a string.
                plaintext = srDecrypt.ReadToEnd();
            }
            finally
            {
                // Clean things up.

                // Close the streams.
                if (srDecrypt != null)
                    srDecrypt.Close();
                if (csDecrypt != null)
                    csDecrypt.Close();
                if (msDecrypt != null)
                    msDecrypt.Close();

                // Clear the RijndaelManaged object.
                if (aesAlg != null)
                    aesAlg.Clear();
            }

            return plaintext;

        }
        /// <summary>
        /// Calcula o hash MD5 da string input
        /// </summary>
        /// <param name="input">string de input para calcular o hash MD5</param>
        /// <returns>Retorna o hash MD5</returns>
        public static string CalculateMD5Hash(string input)
        {
            // step 1, calculate MD5 hash from input
            MD5 md5 = MD5.Create();
            byte[] inputBytes = System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("x2"));
            }
            return sb.ToString().ToUpper();
        }
        /// <summary>
        /// Construtor estatico
        /// </summary>
        static Util()
        {
            Key = ToByteArray("7624fd0c34c42da20512c0df0a321230");
            IV = ToByteArray("00000000000000000000000000000000");
        }
        #endregion
    }
}